const sha1 = require('sha1')
const {jsdata} = require('./client')
const config = {
    token: 'liuyonghtmlwechat',
    appID: 'wx97570034fa2d5f07',
    appsecret: '88901fcef2851e5789b791fd3122a521'
}

/*
目的： 计算出signatrue 微信加密签名 和传过来的进行对比 一致就是微信服务器
1将参与微信加密前面的三个参数 时间戳 随机数字 token 测试官网
2数组所有参数拼接字符串 进行sha1加密
3加密生成 对比 一样 就 返回echostr 给服务器
不一样就给error
*/
/*

<xml><ToUserName><![CDATA[gh_380400827dc2]]></ToUserName>//开发者id
<FromUserName><![CDATA[o7BPn6fli2GF8UIv9pduWmoQ6FRU]]></FromUserName>用户openid
<CreateTime>1618214329</CreateTime> 时间戳
<MsgType><![CDATA[text]]></MsgType>消息类型
<Content><![CDATA[1]]></Content>发送内容
<MsgId>23167258496361246</MsgId> 消息id 微信默认服务器会默认存三天
</xml>

{
  signature: '071b06a1ebe1768add207723a77f7170b37c40ed',
  timestamp: '1618214329',
  nonce: '1305335332',
  openid: 'o7BPn6fli2GF8UIv9pduWmoQ6FRU'
}
*/
exports.moddle = async (req,res,nexy)=>{
    let {signature,echostr,timestamp,nonce} = req.query && req.query
    let {token} = config
    const arr = [timestamp,nonce,token];
    const arrsort = arr.sort()
    const str = arr.join('')
    const sha1str = sha1(str)
    
    /*
     微信服务器发送两种类型给开发者服务器
     get是验证服务器的有效
     post是微信服务器将用户的数据发给开发者
    */
   if(req.method === 'GET') {
    if(sha1str === signature) {
        res.send(echostr)
    } else {
        res.end('error')
    }
   } else if (req.method === 'POST') {
    if(sha1str === signature) {
        let pro = function () {
            return new Promise((resovle,reject)=>{
                let xmldata = ''
            req.on('data',data=>{
                xmldata += data.toString()
            })
            req.on('end',()=>{
                console.log('完毕')
                resovle(xmldata)
            })
            })
        }
        let data = await pro()
        let jsobj = await jsdata(data)
        let obj = {}
        if(typeof jsobj.xml === 'object'){
            Object.keys(jsobj.xml).map(res=>{
                obj[res] = jsobj.xml[res][0]
            })
        }
        console.log(obj)
        let content = ''
        if(obj.MsgType === 'text') {
            if(obj.Content === '1') {
                content = '123'
            } else if (obj.Content.match('爱'))
            {
                content = '爱'
            }
        }
        // /static/a.jpg
        let xml = `<xml>
        <ToUserName><![CDATA[${obj.FromUserName}]]></ToUserName>
        <FromUserName><![CDATA[${obj.ToUserName}]]></FromUserName>
        <CreateTime>${Date.now()}</CreateTime>
        <MsgType><![CDATA[news]]></MsgType>
  <ArticleCount>1</ArticleCount>
  <Articles>
    <item>
      <Title><![CDATA[title1]]></Title>
      <Description><![CDATA[description1]]></Description>
      <PicUrl><![CDATA[picurl]]></PicUrl>
      <Url><![CDATA[http://3wmm57.natappfree.cc/static/a.jpg]]></Url>
    </item>
  </Articles>
      </xml>`
      let img = `<xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>12345678</CreateTime>
      <MsgType><![CDATA[image]]></MsgType>
      <Image>
        <MediaId><![CDATA[media_id]]></MediaId>
      </Image>
    </xml>`
        console.log(req.query)
        res.send(xml)
       
    }
    
   } 
   nexy()
}